package admincow.servlet.vaca;

import java.io.IOException;
import java.text.DecimalFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admincow.dao.VacaDAO;
import admincow.vo.VacaVO;


 public class VacaInsertarServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public VacaInsertarServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String a = request.getParameter("RPvaca");
		int v=a.length();
		if (v==4){
			System.out.println("Yes");
			VacaDAO pao= new VacaDAO();
			VacaVO vi = pao.codigo(a);
			request.setAttribute("codigo", vi);

			VacaVO cod= (VacaVO)request.getAttribute("codigo");        
	                   String r = cod.getRPvaca();
	                   String m;
	                   System.out.println("recuperando");
	                   System.out.println(r);
	                   int nro;
	                   nro=Integer.parseInt(r);
	                   DecimalFormat sd= new DecimalFormat("0000");
	                   m=a+sd.format(nro);
						System.out.println("Pruebamelo");
						System.out.println(m);
						request.setAttribute("covaca", m);
						
			RequestDispatcher rd = request
					.getRequestDispatcher("/vacaNuevoo.jsp");
			rd.forward(request, response);
		}else{
			System.out.println("NO");
			String b = request.getParameter("idraza");
			String c = request.getParameter("nombre");
			String d = request.getParameter("fech_nac");
			String e = request.getParameter("registro");
			String f = request.getParameter("fech_reg");
			String g = request.getParameter("RPpadre");
			String h = request.getParameter("RPmadre");
			String i = request.getParameter("etapa");
			String j = request.getParameter("estado");
			String k = request.getParameter("ordenio");
			

			VacaVO vo = new VacaVO();
			vo.setRPvaca(a);
			System.out.println(a);
			
			vo.setIdraza(Integer.parseInt(b));
			System.out.println(b);
			
			vo.setNombre(c);
			System.out.println(c);
			
			vo.setFech_nac(d);
			System.out.println(d);
			
			vo.setRegistro(Integer.parseInt(e));
			System.out.println(e);
			
			vo.setFech_reg(f);
			System.out.println(f);
			
			vo.setRPpadre(g);
			System.out.println(g);
			
			vo.setRPmadre(h);
			System.out.println(h);
			
			vo.setEtapa(i);
			System.out.println(i);
			
			vo.setEstado(j);
			System.out.println(j);
			
			vo.setOrdenio(k);
			System.out.println(k);
			
			VacaDAO dao = new VacaDAO();
			dao.insertar(vo);

			RequestDispatcher rd = request
					.getRequestDispatcher("/vacaBuscar.jsp");
			rd.forward(request, response);
		}
			
			
			
			
			
		
		
	}   	  	    
}